from rich.console import Console
from rich.syntax import Syntax
from rich.panel import Panel
import requests

console = Console()

def fetch_api(url):
    try:
        response = requests.get(url)
        response.raise_for_status()
        return response.text
    except Exception as e:
        return f"[bold red]错误:[/bold red] {str(e)}"

def analyze_api():
    url = Prompt.ask("输入API地址", default="https://api.github.com")
    content = fetch_api(url)
    
    # 自动检测内容类型
    if "application/json" in response.headers.get("Content-Type", ""):
        syntax = Syntax(
            content,
            "json",
            theme="monokai",
            line_numbers=True,
            word_wrap=True
        )
    else:
        syntax = Syntax(
            content,
            "http",
            theme="github-dark",
            line_numbers=False
        )
    
    console.print(
        Panel.fit(
            syntax,
            title=f"API 响应 ({url})",
            border_style="bright_cyan"
        )
    )

analyze_api()